import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:wayofdressing/contraints.dart';
import 'package:wayofdressing/all_information/user_information.dart';
import 'package:wayofdressing/all_widget/head_image.dart';
import 'package:wayofdressing/good_method/formatDate.dart';
import 'package:wayofdressing/screens/choose_gender_sheet.dart';
import 'package:wayofdressing/screens/edit_line_information_screen.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';
import 'package:wayofdressing/dio/UserDetailDio.dart';
import 'package:wayofdressing/dio/configureDio.dart';

class EditSelfInformation extends StatefulWidget {
  @override
  State<EditSelfInformation> createState() => _EditSelfInformationState();
}

class _EditSelfInformationState extends State<EditSelfInformation> {
  String title = '';
  DateTime selectedDate = DateTime.now();

  _cupertinoPicker() {
    DatePicker.showDatePicker(
      context,
      pickerTheme: DateTimePickerTheme(
        showTitle: true,
        confirm: Text('确定', style: TextStyle(color: Colors.red)),
        cancel: Text('取消', style: TextStyle(color: Colors.cyan)),
      ),
      minDateTime: DateTime.parse('1949-10-01'),
      //起始日期
      maxDateTime: DateTime.now(),
      //终止日期
      initialDateTime: selectedDate,
      //当前日期
      dateFormat: 'yyyy-MMMM-dd',
      //显示格式
      locale: DateTimePickerLocale.zh_cn,
      //语言 默认DateTimePickerLocale.en_us
      onClose: () => print("----- onClose -----"),
      onCancel: () => print('onCancel'),
      onChange: (dateTime, List<int> index) {
        //改变的时候
        setState(() {
          selectedDate = dateTime;
        });
      },
      onConfirm: (dateTime, List<int> index) {
        //确定的时候
        setState(() {
          selectedDate = dateTime;
          upgradeBirthday();
        });
      },
    );
  }

  Future<void> upgradeBirthday() async {
    FocusScope.of(context).unfocus();
    await updateUserDetailByUserId(
        dio: dio, userId: userId, birthday: formatDate(selectedDate) ?? ' ');
    Response response = await getUserDetailByUserId(userId: userId, dio: dio);
    dynamic data = response.data['data'];
    setState(() {
      birthday = data['birthday'] ?? '';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      endDrawer: EditLineInformationScreen(
        title: title,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Stack(
            clipBehavior: Clip.none,
            alignment: Alignment(0, 0),
            children: [
              Container(
                height: MediaQuery.of(context).size.height - 280,
                decoration: BoxDecoration(
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withOpacity(0.6),
                        // 阴影颜色和透明度
                        spreadRadius: 2,
                        // 阴影扩展半径
                        blurRadius: 5,
                        // 阴影模糊半径
                        offset: Offset(0, 6), // 阴影偏移量
                      ),
                    ],
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(25)),
                child: Column(
                  children: [
                    SizedBox(
                      height: 58,
                    ),
                    Builder(
                      builder: (context) => LineButton(
                        title: '名字',
                        content: name,
                        onPressed: () {
                          setState(() {
                            title = '名字';
                          });
                          Scaffold.of(context).openEndDrawer();
                        },
                      ),
                    ),
                    Builder(
                      builder: (context) => LineButton(
                        title: '简介',
                        content: bio,
                        onPressed: () {
                          setState(() {
                            title = '简介';
                          });
                          Scaffold.of(context).openEndDrawer();
                        },
                      ),
                    ),
                    LineButton(
                      title: '性别',
                      content: gender,
                      onPressed: () {
                        showModalBottomSheet(
                                context: context,
                                builder: (context) => ChooseGenderSheet())
                            .then((_) {
                          setState(() {});
                        });
                      },
                    ),
                    LineButton(
                      title: '生日',
                      content: birthday,
                      onPressed: () {
                        _cupertinoPicker();
                      },
                    ),
                    Builder(
                      builder: (context) => LineButton(
                        title: '所在地',
                        content: location,
                        onPressed: () {
                          setState(() {
                            title = '所在地';
                          });
                          Scaffold.of(context).openEndDrawer();
                        },
                      ),
                    ),
                    Builder(
                      builder: (context) => LineButton(
                        title: '学校',
                        content: school,
                        onPressed: () {
                          setState(() {
                            title = '学校';
                          });
                          Scaffold.of(context).openEndDrawer();
                        },
                      ),
                    ),
                    Builder(
                      builder: (context) => LineButton(
                        title: '账号',
                        content: account,
                        onPressed: () {
                          setState(() {
                            title = '账号';
                          });
                          Scaffold.of(context).openEndDrawer();
                        },
                      ),
                    ),
                  ],
                ),
              ),
              Positioned(
                top: -kHeadRadius,
                child: CircleAvatar(
                  backgroundColor: Colors.black,
                  backgroundImage: headImage,
                  radius: kHeadRadius,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

class LineButton extends StatelessWidget {
  LineButton(
      {required this.onPressed, required this.title, required this.content});

  final void Function() onPressed;
  String title;
  String content;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPressed,
      child: Container(
        color: Colors.transparent,
        margin: EdgeInsets.symmetric(vertical: 10),
        child: Row(
          children: [
            SizedBox(
              width: 20,
            ),
            Expanded(
              flex: 2,
              child: Text(
                title,
                style: TextStyle(color: Colors.grey, fontSize: 16),
              ),
            ),
            Expanded(
              flex: 5,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    content,
                    style: TextStyle(color: Colors.black, fontSize: 16),
                  ),
                  Icon(Icons.arrow_forward_ios_rounded)
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
